<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 面包屑 -->
        <el-breadcrumb v-if="this.$route.query.id" separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{this.$route.query.name}}</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
           <!-- 分割线 -->
        <el-divider></el-divider>
        </el-breadcrumb>
        <el-form
          :inline="true"
          ref="from"
          :model="query"
          class="demo-form-inline"
        >
          <el-form-item label="标签名称">
            <el-input
              v-model="query.tagName"
              placeholder="请输入标签名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select placeholder="请选择" v-model="query.state">
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button class="el-button" @click="delval">清除</el-button>
            <el-button type="primary" @click="onSubmit">搜索</el-button>
          </el-form-item>
          <el-form-item class="rightTab">
            <el-button type="text" v-if="subject" @click="backRouter"
              >←返回学科</el-button
            >
            <el-button class="el-buttonx" type="success" @click="addTags">
              <i class="el-icon-edit"></i>新增目录</el-button
            >
          </el-form-item>

          <el-row>
            <el-col :span="24">
              <div class="grid-content bg-purple-dark">
                <i class="el-alert__icon el-icon-info"></i>
                <span class="tagsSize">数据一共{{ total }}条</span>
              </div>
            </el-col>
          </el-row>

          <!-- 表格 -->
          <el-table :data="tableData" style="width: 100%">
            <el-table-column type="index" label="序号" width="100">
            </el-table-column>
            <el-table-column prop="subjectName" label="所属学科" width="220">
            </el-table-column>
            <el-table-column prop="tagName" label="标签名称" width="220">
            </el-table-column>
            <el-table-column prop="username" label="创建者" width="220">
            </el-table-column>
            <el-table-column prop="addDate" label="创建日期" width="220">
            </el-table-column>
            <el-table-column prop="state" label="状态" width="220">
              <template slot-scope="scope">
                {{ scope.row.state == "1" ? "以启用" : "以禁用" }}
              </template>
            </el-table-column>
            <el-table-column prop="handle" label="操作" width="220">
              <template slot-scope="scope">
                <el-button
                  @click="stateChange(scope.row.id)"
                  v-if="scope.row.state == '1'"
                  type="text"
                  >启用</el-button
                >
                <el-button
                  v-else
                  type="text"
                  @click="stateChangeRun(scope.row.id)"
                  >禁用</el-button
                >
                <el-button
                  @click="changeTags(scope.row)"
                  :disabled="!scope.row.state == '1'"
                  type="text"
                  >编辑</el-button
                >
                <el-button
                  type="text"
                  :disabled="!scope.row.state == '1'"
                  size="small"
                  @click="delTags(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            class="pagination"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="1000"
            background
            layout="prev, pager, next,sizes,jumper"
            :total="total"
          >
          </el-pagination>
          <el-dialog
            v-if="dialogVisible"
            @close="dialogClose()"
            title="新增目录"
            :visible.sync="dialogVisible"
            width="22%"
          >
            <!-- :label="" -->

            <tagsadd
              :tagDate="tagDate"
              :isEdit="isEdit"
              :tagId="tagId"
              ref="restForm"
              @close="dialogclose"
              @changeData="changeData()"
            ></tagsadd>
          </el-dialog>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/tags.js'
import { status } from '@/api/hmmm/constants.js'
import tagsadd from '../components/tags-add.vue'
export default {
  name: 'Tags',
  data () {
    return {
      // form: {
      //   id: '',
      //   state: '', // 状态
      //   subjectID: '', // 学科id
      //   subjectName: '', // 学科名称
      //   tagName: '', // 标签名称
      //   totals: '', // 数据总数
      //   username: ''// 创建者名称
      // },

      formInline: {},
      total: 0,
      tableData: [], // 列表数据
      query: {
        // 分页数据
        page: '',
        pagesize: '',
        tagName: '',
        state: ''
      },
      dialogVisible: false,
      tagDate: {
        tagName: '', // 标签名称
        subjectID: '' // 学科id
      },
      // stateDate: { // 状态数据
      //   id: '',
      //   state: ''
      // },
      status: status, // 状态数据枚举

      isEdit: false, // 编辑状态
      tagId: '', // 修改标签ID
      subject: false // 控制按钮显示隐藏
    }
  },
  components: {
    tagsadd
  },
  created () {
    // 调用获取标签详情数据的方法
    this.list()
    // 修改标签
    // this.onSubmit()
  },
  methods: {
    // 返回学科按键
    backRouter () {
      this.$router.go(-1)
    },
    // 清除搜索框
    async delval () {
      this.query.tagName = ''
      this.query.state = ''
      // this.$refs.from.resetFields()
      // const res = await list(this.query)
      // this.tableData = res.data.items
      this.list()
    },
    // 修改标签
    async changeTags (data) {
      this.tagDate.subjectID = data.subjectID
      this.tagDate.tagName = data.tagName
      // console.log(data)
      this.dialogVisible = true
      this.isEdit = true
      this.tagId = data.id
    },
    // 关闭弹框后清空表单数据
    dialogClose () {
      this.dialogVisible = false
      this.$refs.restForm.restForm()
    },
    // 添加标签后重新刷新数据
    changeData () {
      this.list()
      this.dialogVisible = false
    },
    // 删除标签的方法
    async remove (data) {
      await remove(data)
      this.list()
      this.$message.success('删除成功')
    },
    // 用户点击删除按钮
    delTags (data) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.remove(data)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 修改启用禁用状态(启用)
    async stateChange (id) {
      await changeState({ id })
      this.$message.success('操作成功')
      this.list()
    },
    // 修改启用禁用状态(禁用)
    async stateChangeRun (id) {
      await changeState({ id, state: '1' })
      this.$message.success('操作成功')
      this.list()
    },

    // 新增标签
    addTags () {
      // this.tagDate = {
      //   tagName: '', // 标签名称
      //   subjectID: '' // 学科id
      // }
      this.isEdit = false
      this.dialogVisible = true
    },

    // 当前页数发生变化所触发的事件
    handleCurrentChange (val) {
      this.query.page = val
      this.list()
    },
    // 当前页码大小发生变化时触发的事件
    handleSizeChange (val) {
      this.query.pagesize = val
      this.list()
    },
    // 获取标签详情数据
    async list () {
      if (this.$route.query.id || this.$route.query.name) {
        const params = this.$route.query
        // console.log(params)
        const res = await list({ ...this.query, subjectID: params.id })
        this.tableData = res.data.items
        this.total = res.data.counts
        this.subject = true
      } else {
        const res = await list(this.query)
        this.tableData = res.data.items
        this.total = res.data.counts
        // console.log(res.data)
        // console.log(res)
        // console.log(this.tableData)
      }
    },
    // 搜索按钮
    async onSubmit () {
      this.query.page = 1
      const res = await list(this.query)
      // console.log(res)
      this.tableData = res.data.items

      this.list()
    },
    // 关闭弹框
    dialogclose () {
      this.dialogVisible = false
      this.isEdit = false
    }
  }
}
</script>

<style scoped>
* {
  /* font-size: 12px; */
  /* padding: 0;
  margin: 0; */
}

.rightTab {
  padding-left: 550px;
}
.grid-content {
  color: #909399;
  padding-top: 10px;
  padding-left: 15px;

}
.pagination {
  float: right;
  margin-top: 20px;
  margin-bottom: 10px;
}
.el-alert__icon {
  padding-right: 20px;
  font-size: 14px;
  width: 16px;
}
.el-row {
  margin-bottom: 20px;
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #f4f4f5;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.el-button {
  padding: 9px 15px;
  font-size: 12px;
  border-radius: 3px;
  display: inline-block;
}
.el-buttonx {
  width: 97px;
  height: 32px;
}
</style>
